package itsm.isperp.module.controller.project;

import itsm.isperp.framework.data.domain.DataResponse;
import itsm.isperp.framework.data.domain.PageResponse;
import itsm.isperp.framework.web.controller.BaseController;
import itsm.isperp.framework.web.request.JqGridFilterRule;
import itsm.isperp.framework.web.request.JqGridFilterRuleOp;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.framework.web.response.ResultMessage;
import itsm.isperp.module.entity.project.ProjProjectService;
import itsm.isperp.module.entity.serviceCatalog.SrcService;
import itsm.isperp.module.service.project.ProjProjectServiceService;

import java.util.Arrays;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 项目服务目录控制器
 * 
 * @author lizx
 * @date 2013-2-26
 * @version V1.0
 */
@Controller
@RequestMapping("project/service")
public class ProjProjectServiceController extends
		BaseController<ProjProjectService> {

	/**
	 * 项目服务
	 */
	@Autowired
	protected ProjProjectServiceService projProjectServiceService;

	@Override
	public ProjProjectServiceService getService() {

		return projProjectServiceService;
	}

	@Override
	public String getPageTitle() {
		return "项目服务";
	}

	/**
	 * 保存项目在服务
	 * 
	 * @param projectId
	 *            项目Id列表，各个id用“，”号隔开，并且最后一个字符不能为“，”
	 * @return 保存结果
	 */
	@ResponseBody
	@RequestMapping("saveServices")
	public ResultMessage saveServices(String projectId, String serviceIds) {
		String[] strs = serviceIds.split(",");
		if (strs.length > 0) {
			this.projProjectServiceService.saveProjectServices(projectId,
					Arrays.asList(strs));
		}
		return ResultMessage.newInstance(true);
	}

	/**
	 * 项目服务列表
	 * 
	 * @param request
	 *            请求，须包含projectId的过滤
	 * @param req
	 *            http的请求
	 * @return json数据集
	 */
	@Override
	@ResponseBody
	@RequestMapping("listDetail")
	public PageResponse<Map<String, Object>> listDetail(JqGridRequest request,
			HttpServletRequest req) {
		String projectId = req.getParameter("projectId");
		if (StringUtils.isEmpty(projectId)) {
			return null;
		}
		request.addFilterRule(new JqGridFilterRule("proj_project_id",
				JqGridFilterRuleOp.eq, projectId));
		return this.projProjectServiceService.findAllDetailByRequest(request)
				.toPage();

	}

	/**
	 * 项目管理选择项目
	 * 
	 * @param request
	 * @param req
	 * @return
	 */
	@ResponseBody
	@RequestMapping("selectList")
	public DataResponse<SrcService> selectList(JqGridRequest request,
			HttpServletRequest req) {
		String projectId = req.getParameter("projectId");

		DataResponse<SrcService> result = this.projProjectServiceService
				.findAllServiceByRequest(projectId, request);

		return result;
	}

	@ResponseBody
	@RequestMapping("getByProjectId")
	public DataResponse<SrcService> getByProjectId(String projectId) {

		JqGridRequest request = new JqGridRequest();
		request.isPageable(false);
		request.setSelectColumns("id", "code","name");
		DataResponse<SrcService> result = this.projProjectServiceService
				.getByProjectId(projectId, request);

		return result;
	}

}
